package com.choosefine.it.team.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.IntegerUtil;
import com.choosefine.it.team.model.TeamWorkersCollection;
import com.choosefine.it.team.model.vo.TeamWorkersCollectionVO;
import com.choosefine.it.team.service.TeamWorkersCollectionService;
import com.choosefine.it.worker.dao.FindWorkMapper;
import com.choosefine.it.worker.model.FindWork;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/team/teamWorkersCollection")
@Api(value = "班组收藏工人相关", description = "潘钱钦")
public class TeamWorkersCollectionController extends BaseController {
	Logger logger = LoggerFactory.getLogger(TeamWorkersCollectionController.class);
	
	@Autowired
	private TeamWorkersCollectionService teamWorkersCollectionService;
	@Autowired
	private FindWorkMapper findWorkMapper;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Value("#{yaml.get('worker.iteamInfo.teamCount')}")
	String findWorkURL = null;

	@Value("#{yaml.get('basedata.userinfo.getUserByUserCode')}")
	String userinfoURL = null;
	@Value("#{yaml.get('basedata.workType.getWorkTypeById')}")
	String workTypeUrl = null;
	@Autowired
	CustomRestTemplate restTemplate;

	@Autowired
	ObjectMapper jsonObjectMapper;


	@ApiOperation(value = "获取班组收藏工人的列表(毛俊)", notes = "根据userCode来查询班组收藏工人的列表", response = TeamWorkersCollectionVO.class)
	@ApiImplicitParams({@ApiImplicitParam(name = "userCode", value = "用户编号", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "用户的uuid", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", required = false, dataType = "int", paramType = "query")})
	@RequestMapping(value = "/selectWorkerCollectList/{userCode}/{uuid}", method = RequestMethod.GET)
	public String selectWorkerCollectList(@PathVariable String userCode, @PathVariable String uuid, Integer pageNum, Integer pageSize) throws JsonProcessingException {
		String result = null;
		Map<String,Object> params= new HashMap<String,Object>();
		params.put("userCode",userCode);
		params.put("uuid",uuid);
		params.put("type", CommonConstant.TEAM_COLLECT_WORKER);
		PageDTO pageDTO= new PageDTO();
		pageDTO.setPageSize(pageSize);
		pageDTO.setPageNum(pageNum);
		Page<TeamWorkersCollectionVO> list = teamWorkersCollectionService.selectWorkersCollectionByUserCode(params,pageDTO);
		if(list.size()>0){
			result =responseSuccess(list);
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}

	@ApiOperation(value = "班组取消收藏某个收藏工人信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "班组的code", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "workerPulishId", value = "工人发布找工作的id", required = true, dataType = "Integer", paramType = "path")
	})
	@RequestMapping(value = "/cancleCollection/{userCode}/{uuid}/{workerPulishId}", method = RequestMethod.PUT)
	public String cancleCollection(@PathVariable String userCode, // 用户id
								   @PathVariable String uuid,
								   @PathVariable Integer workerPulishId) // 被收藏对象id
			throws BusinessException {
			String result = null;
			Map<String, Object> map = new HashMap<>();
				map.put("userCode",userCode);
				map.put("uuid",uuid);
				map.put("publishId",workerPulishId);
				map.put("type", CommonConstant.TEAM_COLLECT_WORKER);
				map.put("utime",System.currentTimeMillis());
				Integer isSuccess = teamWorkersCollectionService.deleteCollection(map);
			if (isSuccess.longValue() > 0) {
				result = responseSuccess(isSuccess);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_ERROR);
			}
		return result;
	}

	/**
	 * 11.15 dzq 插入收藏表数据  yuxin-update2.25
	 */
	@ApiOperation(value = "班组收藏工人发布信息")
	@RequestMapping(method = RequestMethod.POST,value = "addTeamCollectionWorker/{userCode}/{uuid}/{workerPulishId}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "班组的code", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "workerPulishId", value = "工人发布找工作的id", required = true, dataType = "Integer", paramType = "path")
	})
	public String addTeamCollectionWorker(@PathVariable String userCode, // 用户id
					  @PathVariable String uuid,
					  @PathVariable Integer workerPulishId, // 被收藏对象id
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String result = null;
		if (userCode == null || uuid == null || workerPulishId == null) {
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		Map map=new HashMap();
		map.put("userCode",userCode);
		map.put("uuid",uuid);
		map.put("publishId",workerPulishId);
		map.put("type",CommonConstant.TEAM_COLLECT_WORKER);
		map.put("ctime",System.currentTimeMillis());
		int num = teamWorkersCollectionService.insertSelective(map);
		if (num == 1) {
			result = responseSuccess(num);
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_CREATE_ERROR);
		}
		return result;
	}

	@ApiOperation(value = "根据该条收藏记录的id查询信息详情", notes = "", response = FindWork.class)
	@ApiImplicitParam(name = "id", value = "收藏记录的id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/getWorkerDetail/{id}", method = RequestMethod.GET)
	public String getWorkerDetail(@PathVariable Integer id) {
		String result = null;
		if (IntegerUtil.isNotZero(id)) {
			TeamWorkersCollection t = teamWorkersCollectionService.selectbyId(id);
			Integer workerpulishid = t.getWorkerpulishid();
			FindWork findWork = mongoTemplate.findOne(new Query(Criteria.where("_id").is(workerpulishid)),
					FindWork.class);

			if (findWork != null) {
				result = responseSuccess(findWork, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
}
